<?php

declare(strict_types=1);

namespace Service\ExtraService;

/**
 * @author chenshubo
 * 重写 Meta 生成器 
 */
use Hyperf\Snowflake\IdGenerator;

class SnowFlakeService
{
    /**
     * @var IdGenerator\SnowflakeIdGenerator
     */
    protected $idGenerator;
    public function __construct(IdGenerator\SnowflakeIdGenerator $idGenerator)
    {
        $this->idGenerator = $idGenerator;
    }
    
    /**
     * 获取订单生成数据ID
     */
    public function getOmsIDGenerate()
    {
        $config = config('snowflake.oms');
        $beginTimesStamp = (int)($config['begin_second'].'000');
        $meta = $this->idGenerator->getMetaGenerator()->generate();
        $meta->setBeginTimesTamp($beginTimesStamp);
        return (string)$this->idGenerator->generate($meta);
    }
    
    /**
     * 获取商品生成数据ID
     */
    public function getGoodsIDGenerate()
    {
        $config = config('snowflake.goods');
        $beginTimesStamp = (int)($config['begin_second'].'000');
        $meta = $this->idGenerator->getMetaGenerator()->generate();
        $meta->setBeginTimesTamp($beginTimesStamp);
        return (string)$this->idGenerator->generate($meta);
    }
    
    /**
     * 获取日志写入数据ID
     * @return string
     */
    public function getLogIDGenerate()
    {
        $config = config('snowflake.log');
        $beginTimesStamp = (int)($config['begin_second'].'000');
        $meta = $this->idGenerator->getMetaGenerator()->generate();
        $meta->setBeginTimesTamp($beginTimesStamp);
        return (string)$this->idGenerator->generate($meta);
    }
    
    /**
     * 获取其它服务数据ID
     * @return string
     */
    public function getOtherIDGenerate()
    {
        $config = config('snowflake.other');
        $beginTimesStamp = (int)($config['begin_second'].'000');
        $meta = $this->idGenerator->getMetaGenerator()->generate();
        $meta->setBeginTimesTamp($beginTimesStamp);
        return (string)$this->idGenerator->generate($meta);
    }
    
    /**
     * 反向解析ID
     * @param int $id
     */
    public function degenerate(int $id)
    {
        return $this->idGenerator->degenerate($id);
    }
}